package com.aaa.inter;

import com.aaa.entity.TBook;
import com.aaa.entity.YzhUser;
import com.aaa.service.YzhUserService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInter implements HandlerInterceptor {

   // @Resource
    //private YzhUserService userService;
    @Resource
    private TBook book;


    public LoginInter() {

        //System.out.println(userService.findAllByPage(1,10,new YzhUser()));
        System.out.println("这是一loginInter");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // null
        System.out.println(book);
        // true   符合条件  ->controller
        // false
        //session user
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user==null){
            System.out.println("还没有登录，请登录");
            //
            // 跳转到登录页面
            response.sendRedirect( request.getContextPath()+"/views/login.jsp");
            return false;
        }else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        //
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {


    }
}
